package com.atguigu.gulimall.seckill.controller;

import com.atguigu.common.utils.R;
import com.atguigu.gulimall.seckill.service.SeckillService;
import com.atguigu.gulimall.seckill.to.SeckillSkuRedisTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author 小坏
 * @Date 2021/2/26 18:35
 * @Version 1.0
 * @program: 父工程 gulimall 万物起源之地
 * <p>
 * 秒杀Controller
 */

@Slf4j
@Controller
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    /**
     * 返回当前时间可以参与的秒杀商品信息
     */
    @GetMapping("currentSeckillSkus")
    @ResponseBody
    public R getCurrentSeckillSkus() {
        List<SeckillSkuRedisTo> vos = seckillService.getCurrentSeckillSkus ();
//        log.info (vos.toString ());
        return R.ok ().setDta (vos);
    }


    /**
     * 返回当前时间可以参与的秒杀商品信息
     */
    @GetMapping("/sku/seckill/{skuId}")
    @ResponseBody
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId) {
        SeckillSkuRedisTo to = seckillService.getSkuSeckillInfo (skuId);
        return R.ok ().setDta (to);
    }

    /**
     * 秒杀接口
     * seckill
     * @return
     */

    @GetMapping("/kill")
    public String seckill(@RequestParam("killId") String killId, @RequestParam("key") String key,
                          @RequestParam("num") Integer num, Model model) {
        String orderSn = seckillService.seckill (killId, key, num);
        System.out.println (orderSn);
        //判断登录
        model.addAttribute ("orderSn", orderSn);
        return "success";
    }
}
